using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.TeamFoundation.VersionControl.Client;
using Ltf.Remoting.Operations;
using Ltf.Remoting.TeamFoundation;

namespace Ltf.Diffs.MergeCommands
{
	public class MergeOverwriteFileCommand : MergeCommand
	{
		public MergeOverwriteFileCommand(TFServerConnection connection, string targetFile, string sourceFile)
			: this(connection, targetFile, sourceFile, false)
		{ }

		public MergeOverwriteFileCommand(TFServerConnection connection, string targetFile, string sourceFile, bool backup)
		{
			this.Connection = connection;
			this.sourceFile = sourceFile;
			this.TargetFile = targetFile;
			this.backup = backup;
		}

		private string sourceFile;

		public string SourceFile
		{
			get { return sourceFile; }
			set { sourceFile = value; }
		}

		private bool backup;

		public bool Backup
		{
			get { return backup; }
			set { backup = value; }
		}
	

		public override MergeResult Execute()
		{
			if (backup)
				BackupTargetFile();

			File.SetAttributes(TargetFile, FileAttributes.Normal);
			File.Copy(sourceFile, TargetFile);
			WorkspaceOperators wso = GetWorkspaceOperators();
			if (wso != null)
			{
				wso.PendAdd(new string[] { TargetFile });
			}
			return new MergeResult(0, 0, 0, 1, 0, 0);
		}

		

		public override void Validate()
		{
			if (!File.Exists(sourceFile))
				throw new MergeException(string.Format("Source file '{0}' does not exist.", SourceFile));
			if (!File.Exists(TargetFile))
				throw new MergeException(string.Format("Target file '{0}' does not exists.", TargetFile));
		}

		public override string ShortName
		{
			get { return "OverWrite"; }
		}
	}
}